﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zyg.Utility.Helper;

namespace Zyg.SSO.Web
{
    public partial class AddAccountApp : Zyg.SSO.Web.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request.QueryString["appId"]))
            {
                Response.Redirect("ApList.aspx");
                return;
            }
            else
            {
                bool parseSucceed;

                Guid appId = Request.QueryString["appId"].ToGuid(out parseSucceed);

                if (!parseSucceed)
                {
                    Response.Redirect("ApList.aspx");
                    return;
                }

                Zyg.SSO.Core.Business.App app = Zyg.SSO.Core.Business.App.Load(appId);
                if (app == null)
                {
                    Response.Redirect("ApList.aspx");
                    return;
                }



                Zyg.SSO.Core.Business.Account account = base.Account;
                Zyg.SSO.Core.Business.AccountApp accountApp = new Zyg.SSO.Core.Business.AccountApp();
                accountApp.AccountID = account.Id;
                accountApp.AppID = appId;
                accountApp.ExpireTime = DateTime.MaxValue;
                accountApp.Id = Guid.NewGuid();

                base.Account.Apps.AddOnSave(accountApp);

                base.Account.Save();

                Response.Redirect("ApList.aspx");
            }
        }
    }
}
